extends Node

@export var character: CharacterBody3D = null
@export var camera: Node3D = null
@export var move_speed: float = 30.0
@export var acceleration: float = 15
@export var movement_animation_transition_speed = 0.15

@onready var animation_tree: AnimationTree = $"../../AnimationTree"

var current_input: Vector2
var current_velocity: Vector2

func _process(delta: float) -> void:
	var velocityDelta = (current_input - current_velocity)
	if velocityDelta.length() > movement_animation_transition_speed:
		velocityDelta = velocityDelta.normalized() * movement_animation_transition_speed

	current_velocity += velocityDelta
	var animation_velocity = current_velocity
	animation_velocity.y *= -1
	var velocity_value = maxf(absf(animation_velocity.x), absf(animation_velocity.y))
	if velocity_value != 0 and velocity_value != 1:
		print(velocity_value)
	animation_tree.set("parameters/Movement/UnfocusMovement/blend_position", velocity_value)

func _physics_process(delta: float) -> void:
	move_character(delta)

func move_character(delta: float) -> void:
	current_input = Input.get_vector("move_left", "move_right", "move_up", "move_down") # 获取输入向量
	# 绕指定轴旋转，将输入向量转为角色(即相机)的朝向
	var dir = Vector3(current_input.x, 0, current_input.y).rotated(Vector3.UP, camera.rotation.y)
	character.velocity = lerp(character.velocity, dir * move_speed, delta * acceleration)
	character.move_and_slide()
